package com.ctyon.watchmanager.ui.fragment;

import android.os.Bundle;
import android.os.CountDownTimer;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.ctyon.common.util.DataUtil;
import com.ctyon.common.util.TSnackbarUtil;
import com.ctyon.netlibrary.net.revert.BaseResponseEntity;
import com.ctyon.netlibrary.use.BaseObserver;
import com.ctyon.watchmanager.R;
import com.ctyon.watchmanager.databinding.FragmentRetrieveBinding;
import com.ctyon.watchmanager.model.net.service.NetService;
import com.ctyon.watchmanager.present.RetrievePresenter;
import com.ctyon.watchmanager.ui.BaseFragment;
import com.orhanobut.logger.Logger;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.databinding.DataBindingUtil;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

/**
 * CreateDate：19-6-25 on 上午9:14
 * Describe:
 * Coder: lee
 */
public class RetrieveFragment extends BaseFragment {

    public static final String TAG = "RetrieveFragment";

    private FragmentRetrieveBinding mBinding;
    private CountDownTimer countDownTimer;

    public static RetrieveFragment newInstance() {
        final RetrieveFragment fragment = new RetrieveFragment();
        final Bundle args = new Bundle();
        fragment.setArguments(args);
        return fragment;
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

        mBinding = DataBindingUtil.inflate(inflater, R.layout.fragment_retrieve, container, false);
        mBinding.setRetrievePresenter(retrievePresenter);
        return mBinding.getRoot();

    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
    }

    private final RetrievePresenter retrievePresenter = new RetrievePresenter() {
        @Override
        public void onSubmitClick() {
            String phone = mBinding.getPhone();
            String password = mBinding.getPassword();
            String code = mBinding.getCode();
            Logger.t(LTAG).i("onSubmitClick : phone = " + phone + "  code = " + code);
            if (!DataUtil.checkPhone(phone)) {
                TSnackbarUtil.TSBError(getActivity(), "请输入正确手机号");
                return;
            }
            if (DataUtil.isEmptyOr(code)) {
                TSnackbarUtil.TSBError(getActivity(), "请先获取验证码");
                return;
            }

            NetService.getInstance()
                    .findPassword(phone, password, code)
                    .subscribeOn(Schedulers.io())
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe(new BaseObserver<BaseResponseEntity>() {
                        @Override
                        public void onSuccess(BaseResponseEntity responseEntity) {
                            Logger.t(LTAG).d("onSuccess : " + responseEntity.toString());
                            TSnackbarUtil.TSBSuccess(getActivity(), "重置密码成功");
                        }
                    });

        }

        @Override
        public void onCodeClick() {
            String phone = mBinding.getPhone();
            Logger.t(LTAG).i("onCodeClick : phone = " + phone);
            if (!DataUtil.checkPhone(phone)) {
                TSnackbarUtil.TSBError(getActivity(), "请输入正确手机号");
                return;
            }
            final String method = "findPasswordGetCode";
            if (NetService.getInstance().isDisable(method)) {
                TSnackbarUtil.TSBError(getActivity(), "请稍候...");
                return;
            }
            NetService.getInstance()
                    .findPasswordGetCode(phone)
                    .subscribeOn(Schedulers.io())
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe(new BaseObserver<BaseResponseEntity>() {
                        @Override
                        public void onSuccess(BaseResponseEntity responseEntity) {
                            Logger.t(LTAG).d("onSuccess : " + responseEntity.toString());
                            TSnackbarUtil.TSBSuccess(getActivity(), "短信已发送");
                            if (null == countDownTimer) {
                                initCountDown();
                            }
                            if (mBinding.getCountDown() == null) {
                                countDownTimer.start();
                            }
                        }

                        @Override
                        public String onOver() {
                            return method;
                        }
                    });
        }
    };

    private void initCountDown() {
        countDownTimer = new CountDownTimer(30000, 1000) {
            @Override
            public void onTick(long millisUntilFinished) {
                mBinding.setCountDown(millisUntilFinished / 1000 + "s");
            }

            @Override
            public void onFinish() {
                mBinding.setCountDown(null);
            }
        };
    }

}
